package com.dpiinc.TechPage.menu

import android.content.Context
import android.content.Intent
import android.graphics.drawable.Animatable
import androidx.core.view.ActionProvider
import android.view.ContextThemeWrapper
import android.view.View
import android.widget.ImageView
import com.dpiinc.TechPage.R
import com.dpiinc.TechPage.activity.EditNoteActivity
import com.dpiinc.TechPage.activity.NoteListActivity
import com.dpiinc.TechPage.pen.activity.ConnectPenActivity
import com.dpiinc.TechPage.pen.activity.PenStatusActivity
import com.dpiinc.TechPage.utils.AppUtil
import com.beiji.lib.pen.PenManager
import com.beiji.lib.pen.constants.PenBusTag
import com.beiji.lib.pen.constants.PenConnectState
import com.beiji.lib.pen.model.BatteryLevelWrapper
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.hwangjr.rxbus.thread.EventThread

/**
 *Toolbar 连接菜单
 * Created by X on 2018/7/12.
 */
class MenuConnectActionProvider(context: Context) : ActionProvider(context) {

    private lateinit var iconView: ImageView
    override fun onCreateActionView(): View {
        iconView = ImageView(context)
        iconView.setImageResource(R.mipmap.ic_pen_disconnected)
        val padding = AppUtil.dp2px(16f).toInt()
        iconView.setPadding(padding, padding, padding, padding)
        iconView.setOnClickListener {
            if (PenConnectState.CONNECTING != PenManager.instance.getConnectState()) {
                val fromClassName = (context as ContextThemeWrapper).baseContext.javaClass.name
                val mFrom = when (fromClassName) {
                    NoteListActivity::class.java.name -> ConnectPenActivity.FROM_NOTE_LIST
                    EditNoteActivity::class.java.name -> ConnectPenActivity.FROM_EDIT_NOTE
                    else -> ConnectPenActivity.FROM_NOTE_LIST
                }
                val connectPenIntent = Intent(context, PenStatusActivity::class.java)
                connectPenIntent.putExtra(ConnectPenActivity.PARAM_FROM, mFrom)
                context.startActivity(connectPenIntent)
//                context.startActivity(Intent(context, PenStatusActivity::class.java))
            }
        }
        setConnectState(PenManager.instance.getConnectState())
        return iconView
    }

    private fun startConnectingAnimation() {
        iconView.setImageResource(R.drawable.connecting_pen_animation)
        (iconView.drawable as Animatable).start()
    }

    private fun setConnectState(connectState: PenConnectState) {
        when (connectState) {
            PenConnectState.CONNECTED -> {
                setPenStateIfLowBattery()
            }
            PenConnectState.CONNECTING -> {
                startConnectingAnimation()
            }
            else -> {
                iconView.setImageResource(R.mipmap.ic_pen_disconnected)
            }
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = [(Tag(PenBusTag.CONNECT_STATE))]
    )
    @Suppress("unused")
    fun onConnectStateChanged(connectState: PenConnectState) {
        setConnectState(connectState)
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = [(Tag(PenBusTag.BATTERY_ACHIEVED))]
    )
    @Suppress("unused")
    fun onBatteryAchieved(batteryLevelWrapper: BatteryLevelWrapper) {
        setPenStateIfLowBattery()
    }

    private fun setPenStateIfLowBattery() {
        if (PenManager.instance.isPenLowBattery()) {
            iconView.setImageResource(R.mipmap.ic_pen_connected_low_battery)
        } else {
            iconView.setImageResource(R.mipmap.ic_pen_connected)
        }
    }

}